package site.marqstree.latte.ec.main.personal;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import site.marqstree.latte.core.delegates.bottom.BottomItemDelegate;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.R2;
import site.marqstree.latte.ec.main.personal.address.AddressDelegate;
import site.marqstree.latte.ec.main.personal.list.ListAdapter;
import site.marqstree.latte.ec.main.personal.list.ListBean;
import site.marqstree.latte.ec.main.personal.list.ListItemType;
import site.marqstree.latte.ec.main.personal.order.OrderListDelegate;
import site.marqstree.latte.ec.main.personal.profile.UserProfileDelegate;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.personal
 * 文件名: PersonalDelegate
 * 创建者: marqstree
 * 创建时间: 2020/2/3 14:07
 * 描述: TODO
 */
public class PersonalDelegate extends BottomItemDelegate implements OnItemClickListener {

    public static final String ORDER_TYPE = "ORDER_TYPE";
    private Bundle mArgs = null;
    final List<ListBean> mData = new ArrayList<>();
    final ListAdapter mAdapter = new ListAdapter();

    @BindView(R2.id.rv_personal_setting)
    RecyclerView mRvSettings = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_personal;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mArgs = new Bundle();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initRecyclerView();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        getData();
        mAdapter.setNewData(mData);
    }

    //全部订单点击事件
    @OnClick(R2.id.tv_all_order)
    void onClickAllOrder() {
        mArgs.putString(ORDER_TYPE, "all");
        startOrderListByType();
    }

    //待接收订单点击事件
    @OnClick(R2.id.ll_receive)
    void onClickReceiveOrder() {
        mArgs.putString(ORDER_TYPE, "noreceive");
        startOrderListByType();
    }

    //用户头像点击事件
    @OnClick(R2.id.img_user_avatar)
    void onClickAvatar() {
        getParentDelegate().getSupportDelegate().start(new UserProfileDelegate());
    }

    //开启订单列表Fragment
    private void startOrderListByType() {
        final OrderListDelegate delegate = new OrderListDelegate();
        delegate.setArguments(mArgs);
        getParentDelegate().getSupportDelegate().start(delegate);
    }

    //初始化列表数据
    private void getData(){
        final ListBean address = new ListBean.Builder()
                .setItemType(ListItemType.ITEM_NORMAL)
                .setId(1)
                .setDelegate(new AddressDelegate())
                .setText("收货地址")
                .build();

        final ListBean system = new ListBean.Builder()
                .setItemType(ListItemType.ITEM_NORMAL)
                .setId(2)
                .setText("系统设置")
                .build();
        mData.add(address);
        mData.add(system);
    }

    private void initRecyclerView(){
        //设置RecyclerView
        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mRvSettings.setLayoutManager(manager);
        mRvSettings.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        final ListBean bean = (ListBean) adapter.getData().get(position);
        int id = bean.getId();
        switch (id) {
            case 1:
                getParentDelegate().getSupportDelegate().start(bean.getDelegate());
                break;
            case 2:
                getParentDelegate().getSupportDelegate().start(bean.getDelegate());
                break;
            default:
                break;
        }
    }
}
